//
//  NotificationDetailViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/8/2.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "NotificationDetailViewController.h"
#import <WebKit/WebKit.h>

@interface NotificationDetailViewController ()<WKNavigationDelegate>

@property (nonatomic, strong) NSDictionary *dataDic;

@end

@implementation NotificationDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorWhite;
    [self loadBaseTitleLable:@"通知详情"];
    
    [self getNotificationData];
}
- (void)getNotificationData
{
    if (self.noticeid) {
        NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
        [MBProgressHUD showMessage:nil toView:self.view];
        [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/member/member.ashx?action=noticecontent&companyid=%@&code=%@&timestamp=%@&noticeid=%@",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp],self.noticeid] parameters:nil success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:self.view];
            if ([responseObject[@"status"] intValue] == 1) {
                self.dataDic = responseObject;
                [self setUpUI];
            }
            
        } failure:^(NSError *error) {
            [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        }];
    }
}
- (void)setUpUI
{
    UILabel *label = [UILabel labelWithText:self.dataDic[@"noticeTitle"] textColor:UIColorFromRGB(0x333333) fontSize:15];
    [label sizeToFit];
    label.numberOfLines = 0;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(rateWidth(30));
        make.top.equalTo(self.view).offset(SafeAreaTopHeight);
        make.width.mas_lessThanOrEqualTo(@(kScreenWidth-rateWidth(60)));
        make.height.equalTo(@(rateHeight(100)));
    }];
    
    NSString *str0 = [NSString stringWithFormat:@"%@%@%@",@"<!doctype html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"><style>body,div,p{font-size:14px;line-height:1.5em;}img {width:100%; height:auto;}</style></head><body>",self.dataDic[@"noticeContent"],@"</body></html>"];
    NSString *str = [str0 stringByReplacingOccurrencesOfString:@"src=\"" withString:@"src=\"http:"];
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"src=\"http:http:" withString:@"src=\"http:"];
//    NSLog(@"---%@",str1);
    // 1.创建webview
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(10, SafeAreaTopHeight+rateHeight(100), kScreenWidth-20, kScreenHeight-SafeAreaTopHeight-rateHeight(100))];
    webView.navigationDelegate = self;
    
    [webView loadHTMLString:str1 baseURL:nil];
    // 最后将webView添加到界面
    [self.view addSubview:webView];
    
}
- (void)dealloc
{
    //    NSLog(@"控制器被销毁了");
    [self clearCache]; // 清除缓存
}
#pragma mark - wknavigationdelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    //    NSLog(@"0000000000000");
    [MBProgressHUD showMessage:@""];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    //    NSLog(@"sssssssssssss");
    [MBProgressHUD hideHUD];
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    //    NSLog(@"ffffffffffff");
    [MBProgressHUD hideHUD];
}
- (void)clearCache
{
    if ([[[UIDevice currentDevice] systemVersion] intValue] > 8) {
        NSArray * types =@[WKWebsiteDataTypeMemoryCache,WKWebsiteDataTypeDiskCache]; // 9.0之后才有的
        NSSet *websiteDataTypes = [NSSet setWithArray:types];
        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
        }];
        
    } else {
        NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
        //        NSLog(@"%@", cookiesFolderPath);
        NSError *errors;
        [[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
